<template>
  <div class="container">
    <div class="login-wrapper">
      <div class="header">Register</div>
      <div class="form-wrapper">
        <input
          type="text"
          name="username"
          placeholder="Username"
          class="input-item"
          v-model="regForm.username"
        />
        <input
          type="password"
          name="password"
          placeholder="Password"
          class="input-item"
          v-model="regForm.password"
        />
        <input
          type="password"
          name="rePassword"
          placeholder="Confirm Password"
          class="input-item"
          v-model="regForm.rePassword"
        />
        <input
          type="email"
          name="email"
          placeholder="Email"
          class="input-item"
          v-model="regForm.email"
        />
        <div class="btn" @click="reg">Register</div>
      </div>
      <div class="msg a"  >
        <p class="a">注册成功的用户可用于登录</p>
      </div>
    </div>
  </div>
</template>

  <script>
export default {
  data() {
    return {
      regForm: {
        username: "",
        password: "",
        rePassword: "",
        email: "",
      },
    };
  },
  methods: {
    reg() {
      if (this.regForm.username === "") {
        this.$toast.showMessage("账号不能为空");
        return;
      }
      if (this.regForm.password === "") {
        this.$toast.showMessage("密码不能为空");
        return;
      }
      if (this.regForm.rePassword === "") {
        this.$toast.showMessage("确认密码不能为空");
        return;
      }
      if (this.regForm.email === "") {
        this.$toast.showMessage("邮箱不能为空");
        return;
      }
      if (this.regForm.password !== this.regForm.rePassword) {
        this.$toast.showMessage("两次密码输入必须一致");
        return;
      }
      //发送请求完成注册操作
      //this.$store.commit('user/setUser',this.loginForm)
      const requestData = {
        username: this.regForm.username,
        password: this.regForm.password,
        email: this.regForm.email,
      };

      // 发送注册请求给后端API接口
      this.$http
        .post("register", requestData)
        .then((response) => {
          const data = response.data;
          console.log(data);
          // 处理后端API的响应
          if (data.code === "1") {
            // 注册成功
            console.log(data.data);
            this.$toast.showMessage("注册成功");
            this.$store.commit("user/setUser", data.data);
            this.$router.push("../My");
          } else {
            // 注册失败
            this.$toast.showMessage(data.msg);
          }
        })
        .catch((error) => {
          if (error.response && error.response.status === 400) {
            // 处理用户名已存在的错误
            this.$toast.showMessage(error.response.data.msg);
          } else {
            // 处理其他错误
            console.error("注册失败:", error);
            this.$toast.showMessage("注册失败");
          }
        });
    },
  },
};
</script>
 <style scoped>
.container {
  height: 90vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(
    to right,
    rgba(102, 88, 88, 0.125),
    rgba(156, 211, 219, 0.262)
  );
}
.msg {
  text-align: center;
  line-height: 88px;
  font-size: 12px;
  color: rgba(0, 0, 0, 0.6);
}
.login-wrapper {
  background-color: rgba(255, 255, 255, 0.95);
  width: 390px;
  height: 620px;
  border-radius: 15px;
  padding: 0 50px;
  box-shadow: 0px 10px 20px rgba(0, 0, 0, 0.2);
}

.header {
  font-size: 38px;
  font-weight: bold;
  text-align: center;
  line-height: 200px;
}

.form-wrapper {
  margin-top: 50px;
}

.input-item {
  display: block;
  width: 100%;
  margin-bottom: 20px;
  border: 0;
  padding: 10px;
  border-bottom: 1px solid rgb(128, 125, 125);
  font-size: 15px;
  outline: none;
}

.btn {
  text-align: center;
  padding: 10px;
  margin: 0 auto;
  width: 100%;
  margin-top: 40px;
  background-image: linear-gradient(to right, #a6c1ee, #fbc2eb);
  color: #fff;
  cursor: pointer;
  transition: background-color 0.3s ease;
}

.btn:hover {
  background-image: linear-gradient(to right, #fbc2eb, #a6c1ee);
}
.a {
  text-decoration-line: none;
  color: #abc1ee;
  transition: color 0.3s ease;
}
a:hover {
  color: #fbc2eb;
}
</style>
